import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/entity/category_parent_entity.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';
import 'package:hm_show_flutter/widgets/empty_view.dart';

///二级分类列表
class CategoryChildView extends StatefulWidget {
  final List<CategoryParentDataListCategory> _listChild;

  CategoryChildView(this._listChild);

  @override
  _CategoryChildViewState createState() => _CategoryChildViewState();
}

class _CategoryChildViewState extends State<CategoryChildView> {

  Widget getContentView() {
    return Container(
      color: Colors.grey[100],
      padding: EdgeInsets.only(left: 3),
      child:GridView.builder(
          itemCount: widget._listChild.length,
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3, childAspectRatio: 0.90),
          itemBuilder: (BuildContext context, int index) {
            return getItemView(widget._listChild[index]);
          }),
    );
  }

  Widget getItemView(CategoryParentDataListCategory item) {
    return GestureDetector(
      onTap: (){
        //前往商品列表
        NavigatorUtils.goCategoryGoodsListPage(context, item.name, item.id, item.parentId);
      },
      child: Card(
          margin: EdgeInsets.all(3.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Expanded(
                    child: CachedImageView(double.infinity,
                        ScreenUtil.instance.setHeight(60.0), item.url)),
                Padding(
                  padding: EdgeInsets.only(top: 4.0),
                ),
                Container(
                  padding: EdgeInsets.only(left: 4.0, top: 4.0),
                  alignment: Alignment.center,
                  child: Text(
                    item.name,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Colors.black54, fontSize: 14.0),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 4.0),
                ),
              ],
          )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget._listChild == null ? EmptyView() : getContentView(),
    );
  }
}
